# !apt-get install -qq libgdal-dev libproj-dev
# #!pip install --no-binary shapely shapely
# !pip install --no-binary shapely shapely --force
# !pip install cartopy
import xarray as xr
from datetime import datetime, timedelta
import numpy as np
import pandas as pd
import warnings
import cartopy
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
Now we will use data from "ERA5 hourly data on single levels from 1940 to present" that is availble for free in climate data store website: cds.climate.copernicus.eu
The data I downloaded has the following specifications:
Product type:
Reanalysis
Variable:
2m temperature
Year:2023
Month:June, July
Day:
01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
Time:
00:00, 01:00, 02:00, 03:00, 04:00, 05:00, 06:00, 07:00, 08:00, 09:00, 10:00, 11:00, 12:00, 13:00, 14:00, 15:00, 16:00, 17:00, 18:00, 19:00, 20:00, 21:00, 22:00, 23:00
Sub-region extraction:
North 29°, West 85°, South 18°, East 97°
Format:
NetCDF (experimental)
We can download the data directly using API. we need to follow the setps below to download the data:
!pip install cdsapi
Don't forget to change the output_folder
location as you desire.
import cdsapi
import os
c = cdsapi.Client()
# Specify the folder where you want to save the downloaded data
output_folder = '/mnt/102ECBA62ECB8368/Academic journey/ML_HEATWAVE/Data/Test/'
# Ensure the output folder exists
os.makedirs(output_folder, exist_ok=True)
c.retrieve(
'reanalysis-era5-single-levels',
{
'product_type': 'reanalysis',
'variable': '2m_temperature',
'year': '2023',
'month': [
'06', '07',
],
'day': [
'01', '02', '03',
'04', '05', '06',
'07', '08', '09',
'10', '11', '12',
'13', '14', '15',
'16', '17', '18',
'19', '20', '21',
'22', '23', '24',
'25', '26', '27',
'28', '29', '30',
'31',
],
'time': [
'00:00', '01:00', '02:00',
'03:00', '04:00', '05:00',
'06:00', '07:00', '08:00',
'09:00', '10:00', '11:00',
'12:00', '13:00', '14:00',
'15:00', '16:00', '17:00',
'18:00', '19:00', '20:00',
'21:00', '22:00', '23:00',
],
'area': [
29, 85, 18,
97,
],
'format': 'netcdf',
},
os.path.join(output_folder,'t2m_data_for_exp.nc')
)
nc_file_path='/mnt/102ECBA62ECB8368/Academic journey/ML_HEATWAVE/Data/Test/t2m_data_for_exp.nc'
ds=xr.open_dataset(nc_file_path)
ds
expver=1 means ERA5 and expver=5 means ERA5T. Google to understand more.
ds.t2m.dims
Here we will use just era5(expver=1)
data and we will use data for time = 2023-06-15T12:00:00
expver_slice=1
desired_time = '2023-06-15T12:00:00'
temperature_data=ds.sel(expver=expver_slice,time=desired_time)
temperature_data
Now we have data for 12PM,June 15,2023.We can plot this data in two ways
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
# Get latitude and longitude from xarray.Dataset
latitude = temperature_data['latitude'].values
longitude = temperature_data['longitude'].values
# Get t2m from xarray.Dataset
t2m = temperature_data.t2m
# Create a Cartopy PlateCarree projection
projection = ccrs.PlateCarree()
# Create a figure and axis with Cartopy projection
fig, ax = plt.subplots(subplot_kw={'projection': projection}, figsize=(12, 6))
# Plot the temperature data
pcolormesh=ax.pcolormesh(longitude,latitude,t2m,transform=projection,cmap='coolwarm')
# Add a colorbar
cbar = plt.colorbar(pcolormesh, ax=ax, orientation='vertical', pad=0.07, label='2 m temperature(K)')
# Add coastlines and gridlines
ax.coastlines()
ax.gridlines(draw_labels=True)
ax.add_feature(cartopy.feature.BORDERS)
# Customize plot properties
plt.title('ERA5 2 m Temperature')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
# Show the plot
plt.show()
import pandas as pd
# Get latitude,longitude and t2m from xarray.Dataset
latitude = temperature_data['latitude'].values
longitude = temperature_data['longitude'].values
t2m = np.array(temperature_data['t2m'])
# Reshape the 't2m' data to match the number of latitude and longitude points
reshaped_t2m = t2m.reshape(-1)
# Create a Pandas DataFrame
df = pd.DataFrame({
'latitude': np.repeat(latitude, len(longitude)),
'longitude': np.tile(longitude, len(latitude)),
't2m': reshaped_t2m
})
df
# Import
warnings.filterwarnings("ignore")
# Get the latitude and longitude values
unique_lat = df['latitude'].unique()
unique_lon = df['longitude'].unique()
# Reshape the t2m data into a 2D array matching the grid
t2m_array = df['t2m'].values.reshape(len(unique_lat), len(unique_lon))
# Create a Cartopy PlateCarree projection
projection = ccrs.PlateCarree()
# Create a figure and axis
fig, ax = plt.subplots(subplot_kw={'projection': projection}, figsize=(12, 6))
# Plot the temperature data using pcolormesh
pcolormesh = ax.pcolormesh(unique_lon, unique_lat, t2m_array, transform=projection, cmap='coolwarm')
# Add colorbar
cbar = plt.colorbar(pcolormesh, ax=ax, orientation='vertical', pad=0.05, label='Temperature (K)')
# Set title and labels
ax.set_title('ERA5 2m Temperature')
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
# Add coastlines and gridlines
ax.coastlines()
ax.add_feature(cfeature.BORDERS)
ax.gridlines(draw_labels=True)
# Show the plot
plt.show()
projection = ccrs.PlateCarree()
# Create a figure and axis with Cartopy projection
fig, ax = plt.subplots(subplot_kw={'projection': projection})
# Plot the temperature data using scatter plot
scatter = ax.scatter(df['longitude'], df['latitude'], c=df['t2m'], cmap='coolwarm', s=10, transform=projection)
# Add coastlines and gridlines
ax.coastlines()
ax.gridlines(draw_labels=True)
ax.add_feature(cfeature.BORDERS)
# Customize plot properties
plt.title('Temperature Distribution')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
# Add a colorbar using the scatter mappable
cbar = plt.colorbar(scatter)
cbar.set_label('Temperature')
# Show the plot
plt.show()